package com.bawei.product.service.impl;

import com.bawei.product.domain.PmsSkuImages;
import com.bawei.product.domain.PmsSkuInfo;
import com.bawei.product.domain.PmsSpuInfoDesc;
import com.bawei.product.mapper.PmsSkuInfoMapper;
import com.bawei.product.service.*;
import com.bawei.product.vo.ItemGroupBaseAttoVo;
import com.bawei.product.vo.ItemInfo;
import com.bawei.product.vo.ItemSaleAttr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private IPmsSkuInfoService skuInfoService;

    @Autowired
    private IPmsSkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private IPmsSkuImagesService skuImagesService;

    @Autowired
    private IPmsSpuInfoDescService spuInfoDescService;

    @Autowired
    private IPmsProductAttrValueService productAttrValueService;


    @Autowired
    private ThreadPoolExecutor itemInfoThreadPool;

    @Override
    public ItemInfo getItemBySkuId(Long skuId) throws ExecutionException, InterruptedException {

        ItemInfo itemInfo = new ItemInfo();

        //1
//            2
//            4
//            5
        //3

        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1. 获取skuInfo  40
            PmsSkuInfo pmsSkuInfo = skuInfoService.selectPmsSkuInfoBySkuId(skuId);
            itemInfo.setItemSkuInfo(pmsSkuInfo);

            return pmsSkuInfo;
        },itemInfoThreadPool);

        CompletableFuture<Void> itemSaleAttrCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((res) -> {
            //2. 获取销售属性信息  80
            List<ItemSaleAttr> itemSaleAttrBySpuId = skuSaleAttrValueService.getItemSaleAttrBySpuId(res.getSpuId());
            itemInfo.setItemSaleAttrs(itemSaleAttrBySpuId);
        },itemInfoThreadPool);


        CompletableFuture<Void> skuImagesCompletableFuture = CompletableFuture.runAsync(() -> {
            //3. 获取sku图片信息  60
            List<PmsSkuImages> skuImagesBySkuId = skuImagesService.getSkuImagesBySkuId(skuId);
            itemInfo.setItemSkuImages(skuImagesBySkuId);
        },itemInfoThreadPool);


        CompletableFuture<Void> spuInfoDescCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((res) -> {
            //4. 获取spu描述信息
            PmsSpuInfoDesc pmsSpuInfoDesc = spuInfoDescService.selectPmsSpuInfoDescBySpuId(res.getSpuId());
            itemInfo.setItemSpuInfoDesc(pmsSpuInfoDesc);
        },itemInfoThreadPool);


        CompletableFuture<Void> itemGroupBaseAttrCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((res) -> {
            //5. 获取spu基本属性信息
            List<ItemGroupBaseAttoVo> itemGroupBaseAttrByCatagoryId = productAttrValueService.getItemGroupBaseAttrByCatagoryId(res.getCatalogId());
            itemInfo.setItemGroupBaseAttoVos(itemGroupBaseAttrByCatagoryId);
        },itemInfoThreadPool);


        CompletableFuture.allOf(
                itemSaleAttrCompletableFuture,
                skuImagesCompletableFuture,
                spuInfoDescCompletableFuture,
                itemGroupBaseAttrCompletableFuture).get();  // 阻塞方法




        return itemInfo;
    }
}
